// pages/diagnosisDetail/diagnosisDetail.js
const app = getApp();
const request = require('../../utils/request.js');
const util = require('../../utils/util.js');
const ApiRootUrl = app.globalData.REQUEST_IP_HEAD;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    apiRootUrl: ApiRootUrl,
    detailMessage: {},
    aptId: "",
    medicineList: [],
    mainDisease:[],
    medicineDosage:[],
    medicineUsages:[]
  },

  /**
   * 整理药单信息
   */
  sortMedicine(){
    var detail = this.data.detailMessage;
    if(detail.dosage != null){
      var dosage = detail.dosage.split(",");
      this.setData({
        medicineDosage: dosage,
      })
    }
    if(detail.useway != null){
      var usage1 = detail.useway.split(",");
      var usage2;
      var usageList = [];
      for(var i=0;i<usage1.length;i++){
        usage2 = usage1[i].split("+");
        usageList.push(usage2);
      }
      this.setData({
        medicineDosage: dosage,
        medicineUsages: usageList
      })
    }
  },

  /**
   * 获取病单详情
   */
  getRegistrationDetail(){
    var that = this;
    request.get('/appointment/selectAptDetail', {
      id : that.data.aptId
    },).then((res) => {
      if (res.code == 200) {
        that.setData({
          detailMessage: res.data,
          mainDisease: res.data.symptoms,
          medicineList: res.data.medicineList,
        })
        that.sortMedicine();
      } else {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
      util.showToast(errMsg, 'error');
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      aptId: options.id
    })
    this.getRegistrationDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})